import pymysql


class MySQLOperator:
    def __init__(self):
        self.db = pymysql.connect("192.168.56.11", "wuji1626", "zwh123123", "diary_record")
        self.cursor = self.db.cursor()

    def testdb(self):

        # 使用 execute()  方法执行 SQL 查询
        self.cursor.execute("SELECT VERSION()")

        # 使用 fetchone() 方法获取单条数据.
        data = self.cursor.fetchone()

        print("Database version : %s " % data)

        # 关闭数据库连接
        self.db.close()

    def close(self):
        self.db.close()

    def insert_item(self, task):
        cursor = self.cursor
        task_name = task[1]
        task_type = task[2]
        task_subtype = task[3]
        task_content = task[4]
        task_import_flg = task[5]
        task_plan_flg = task[6]
        task_priority = task[7]
        task_start = task[8]
        task_end = task[9]
        task_gain = task[10]
        task_time = task[11]

        sql = "insert into task_info (task_name, task_type, task_subtype, task_content, task_important_flg, " \
              "task_plan_flg, task_priority, task_start, task_end, task_gain, task_elapse) values ( %s, %s, %s, %s, " \
              "%s, %s, %s, %s, %s, %s, %s)"
        cursor.execute(sql, (task_name, task_type, task_subtype, task_content, task_import_flg, task_plan_flg,
                             task_priority, task_start, task_end, task_gain, task_time))
        self.db.commit()
